#pragma once

#include "../NanoIO.h"
#include <QSerialPort>
#include <QSerialPortInfo>
#include <list>

class SerialPortIO : public NanoIO
{
//类型定义
public:
    typedef struct{
        char portName[32];
        char description[64];
    }SerialPortInfo;

    typedef enum{
        OneStop,
        OneAndHalfStop,
        TowStop
    }SerialPortStopBit;

    typedef enum{
        NoParity,
        EvenParity,
        OddParity,
        SpaceParity,
        MarkParity
    }SerialPortVerify;

//方法定义
public:
    SerialPortIO(SerialPortInfo* info,
                 uint32_t boundRate = 115200,
                 uint8_t dataBit = 8,
                 SerialPortStopBit stopBit = OneStop,
                 SerialPortVerify verify = NoParity);
    virtual ~SerialPortIO();

    virtual bool open();
    virtual bool close();
    virtual bool isOpen();
    virtual int read(uint8_t* data , uint16_t len , uint32_t timeout_ms);
    virtual int write(uint8_t* data , uint16_t len , uint32_t timeout_ms);

    bool setBound(uint32_t bound);
    bool setDataBit(uint8_t dataBit);
    bool setStopBit(SerialPortStopBit stopBit);
    bool setVerifyMode(SerialPortVerify verifyMode);

    static std::list<SerialPortInfo> getSerialPortInfoList();

//私有
private:
    SerialPortInfo      _portInfo;
    uint32_t            _bound;
    uint8_t             _dataBit;
    SerialPortStopBit   _stopBit;
    SerialPortVerify    _verifyMode;

    QSerialPort*   _port;

};

